// server/app/model/job_opening.js
'use strict';

module.exports = app => {
  const { STRING, INTEGER,ENUM , DECIMAL, TEXT, DATE, TINYINT } = app.Sequelize;

  const JobOpening = app.model.define('job_opening', {
    id: {
      type: INTEGER.UNSIGNED,
      primaryKey: true,
      autoIncrement: true
    },
    position_id: {
      type: INTEGER.UNSIGNED,
      allowNull: false,
      comment: '岗位id'
    },
    cuisine_ids: {
      type: STRING,
      comment: '菜系ids',
      get() {
        const rawValue = this.getDataValue('cuisine_ids');
        return rawValue ? rawValue.split(',') : [];
      },
      set(value) {
        this.setDataValue('cuisine_ids', Array.isArray(value) ? value.join(',') : value);
      }
    },
    cuisine_ids_arr: {
      type: app.Sequelize.VIRTUAL,
      get() {
        return this.getDataValue('cuisine_ids');
      }
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      comment: '发布人id'
    },
    work_experience_id: {
      type: STRING,
      comment: '工作经验id'
    },

    work_place: {
      type: STRING(100),
      allowNull: false,
      comment: '工作地点'
    },
    min_salary: {
      type: DECIMAL(12,2).UNSIGNED,
      defaultValue: 0,
      comment: '岗位最小薪资'
    },
    max_salary: {
      type: DECIMAL(12,2).UNSIGNED,
      defaultValue: 0,
      comment: '岗位最大薪资'
    },
    skill_tags: {
      type: STRING(255),
      comment: '技能标签'
    },
    job_requirements: {
      type: TEXT,
      comment: '岗位要求'
    },
    personal_advantage: {
      type: TEXT,
      comment: '个人优势'
    },
    health_ids: {
      type: TEXT,
      comment: '健康证'
    },
    certificates_ids: {
      type: TEXT,
      comment: '证书id'
    },
    qualifications_ids: {
      type: TEXT,
      comment: '资格证id'
    },
    description: {
      type: TEXT,
      comment: '岗位职责'
    },
    benefits: {
      type: TEXT,
      comment: '福利待遇'
    },
    status: {
      type: INTEGER.UNSIGNED,
      defaultValue: 1,
      comment: '状态'
    },
    del_flag: {
      type: TINYINT(1).UNSIGNED,
      defaultValue: 0,
      comment: '删除标记'
    },
    is_urgent: {
      type: TINYINT(1).UNSIGNED,
      defaultValue: 0,
      comment: '收费急聘'
    },
    views_count: {
      type: INTEGER.UNSIGNED,
      defaultValue: 0,
      comment: '浏览次数'
    },
    user_type: {
      type: ENUM('user', 'employer'),
      defaultValue: 'employer'
    },
    application_count: {
      type: INTEGER.UNSIGNED,
      defaultValue: 0,
      comment: '申请人数'
    },
    public_time: {
      type: DATE,
      comment: '发布时间'
    },
    application_deadline: {
      type: DATE,
      comment: '截止时间'
    }
  }, {
    tableName: 'job_openings',
    timestamps: true,
  });

  // 定义关联关系
  JobOpening.associate = function() {
    app.model.JobOpening.belongsTo(app.model.Position, { foreignKey: 'position_id', as: 'position' });
    app.model.JobOpening.belongsTo(app.model.DictionaryDetail, { foreignKey: 'work_experience_id', as: 'workExperience' });
  }
  return JobOpening;
};
